import MySQLdb


def a():
    print()



def query():
    c.execute("select * from student where gender='男'")
    # 数据格式是tuple，防止数据被修改
    all = c.fetchall()
    print(all)
    # fetch要执行多次，需要重新execute
    c.execute("select * from student where gender='男'")
    one = c.fetchone()  # 从all里面抽取第一条显示 None
    print(one)
    c.execute("select * from student where gender='男' limit 1")
    # print(c.fetchall())
    print(c.fetchone())



def delete():
    # c.execute('truncate table student')
    # print(c.fetchall())
    c.execute('delete from student where id=1')
    # 事务提交
    con.commit()
    query()


def insert():
    sql = "INSERT INTO  student (id,name,birth,gender) values (1,'张三','1989-10-10 13:10:10','男')"
    c.execute(sql)
    con.commit()
    query()


def update():
    c.execute("select * from student where  id=1")
    print(c.fetchone())
    sql = "update  student set gender='女' where id=1"
    c.execute(sql)
    con.commit()
    c.execute("select * from student where  id=1")
    print(c.fetchone())

if __name__ == '__main__':
    con = MySQLdb.connect(host='127.0.0.1', user='root', passwd='2940053asd', db='hy')
    c = con.cursor()
    # query()
    # delete()
    # insert()
    update()
